#include "chooselevelscene.h"
#include <QMenuBar>
#include <QPainter>
#include <QDebug>
#include "mypushbutton.h"
#include <QLabel>
#include <QTimer>

ChooseLevelScene::ChooseLevelScene(QWidget *parent)
    : QMainWindow{parent}
{
    //配置选择关卡界面
    this->setFixedSize(320, 590);
    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //设置标题
    this->setWindowTitle("选择关卡");
    //创建菜单栏
    QMenuBar * menubar = menuBar();
    setMenuBar(menubar);
    //创建开始菜单
    QMenu * startMenu = menubar->addMenu("开始");
    //创建退出菜单项
    QAction * quitAction = startMenu->addAction("退出");
    connect(quitAction, &QAction::triggered, [=](){
        this->close();
    });

    //创建 返回按钮
    MyPushButton * btn_back = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    btn_back->setParent(this);
    btn_back->move(this->width() - btn_back->width() - 10, this->height() - btn_back->height() - 10);
    //点击返回
    connect(btn_back, &QPushButton::clicked, [=](){
        //qDebug() << "返回";
        emit clickedBack();
    });

    //显示选择关卡的按钮
    for(int i = 0; i < 20; i++)
    {
        MyPushButton * btn_chooseLevel = new MyPushButton(":/res/LevelIcon.png");
        btn_chooseLevel->setParent(this);
        //设置按钮位置
        btn_chooseLevel->move(25 + i % 4 * 70, 130 + i / 4 * 70);

        //监听每个按钮
        connect(btn_chooseLevel, &QPushButton::clicked, [=](){
//            QString str = QString("选择%1关").arg(i + 1);
//            qDebug() << str;

            //进入关卡页面
            //隐藏关卡选择页面
            this->hide();
            //创建游戏界面
            playscene = new PlayScene(i + 1);
            //调整位置
            playscene->setGeometry(this->geometry());
            //显示
            playscene->show();

            //监听返回按钮
            connect(playscene, &PlayScene::clickedBack, [=](){
                //延时200ms执行
                QTimer::singleShot(200, this, [=](){
                    qDebug() << "返回选关页面";
                    //调整位置
                    this->setGeometry(playscene->geometry());
                    this->show();
                    delete playscene;
                    playscene = nullptr;
                });
            });
        });

        QLabel * label = new QLabel(this);
        label->setFixedSize(btn_chooseLevel->width(), btn_chooseLevel->height());
        label->move(24 + i % 4 * 70, 129 + i / 4 * 70);
        //设置文字 水平居中 和 垂直居中
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        label->setText(QString::number(i + 1));

        //设置label让鼠标穿透（穿透才能点击到按钮）   51号属性（还要记住55号属性，用于设置非模态对话框关闭对话框后释放内存）
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}
//重写绘画事件
void ChooseLevelScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);

    //加载标题
    pix.load(":/res/Title.png");
    painter.drawPixmap(this->width() * 0.5 - pix.width() * 0.5, 30, pix.width(), pix.height(), pix);
}
